﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Models;
using BLL;
using System.Collections.Generic;

namespace PCCS.Management.AdminManage
{
    public partial class CompetDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            if (!IsPostBack)
            {
                if (Session["admin"] != null)
                {
                    Admin admin = AdminManager.GetAdminByAdminID(id);
                    lblAdminName.Text = admin.AdminName;
                    lblAdminDesc.Text = admin.AdminDesc;
                }
                else
                {
                    Response.Write("<script language='javascript'>parent.location.href='../login.aspx';</script>");
                }

            }

            DisplayLeftTypeInfo(id);
        }

        protected void DisplayLeftTypeInfo(int roleId)
        {

            ArrayList arrRoleChildMenu = new ArrayList();
            //根据角色Id和父节点Id得到RoleRight表中已经存在的子菜单信息
            IList<LeftLink> RoleChildRights = LeftLinkManager.GetLeftLinksByUserID3(roleId);
            foreach (LeftLink roleRight in RoleChildRights)
            {
                arrRoleChildMenu.Add(roleRight.TypeID.ToString());
            }

            IList<LeftType> types = LeftTypeManager.GetLeftTypesParent();
            foreach (LeftType type in types)
            {
                string nodeId = type.LeftTypeID.ToString();//ID
                string displayName = type.TypeTitle;//Name
                //加载用户控件
                UserControl roleContol = (UserControl)LoadControl(@"CompetControl.ascx");
                //从用户控件中查找名为"hidParentMenu",并将value设为菜单ID 
                HtmlInputHidden hidParentMenu = (HtmlInputHidden)roleContol.FindControl("hidParentMenu");
                hidParentMenu.Value = nodeId;
                //实例化隐藏域,用以存储角色Id
                HtmlInputHidden hidRoleId = (HtmlInputHidden)roleContol.FindControl("hidRoleId");
                hidRoleId.Value = roleId.ToString();

                //从用户控件中查找"chkParentMenu",将Text属性设为菜单名称
                CheckBox chkParentMenu = (CheckBox)roleContol.FindControl("chkParentMenu");

                chkParentMenu.Text = displayName;

                if (arrRoleChildMenu.Contains(nodeId))
                {
                    chkParentMenu.Checked = true;
                }

                //最后将其添加到控件容器中
                phRoleDistribute.Controls.Add(roleContol);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int roleId = Convert.ToInt32(Request.QueryString["id"]);
            IList<LeftLink> RoleChildRights = LeftLinkManager.GetLeftLinksByUserID(roleId);
            ArrayList arrRoleNodes = new ArrayList();
            //根据角色Id和父节点Id得到RoleRight表中已经存在的子菜单信息
            foreach (LeftLink roleRight in RoleChildRights)
            {
                arrRoleNodes.Add(roleRight.TypeID.ToString());
            }

            foreach (Control ct in phRoleDistribute.Controls)
            {
                CheckBox chk = (CheckBox)ct.FindControl("chkParentMenu");
                HtmlInputHidden hih = (HtmlInputHidden)ct.FindControl("hidParentMenu");

                if (chk.Checked)
                {
                    if (!arrRoleNodes.Contains(hih.Value))
                    {
                        LeftLinkManager.AddLeftLink(roleId, Convert.ToInt32(hih.Value), 0);
                    }
                }
                else
                {
                    if (arrRoleNodes.Contains(hih.Value))
                    {
                        LeftLinkManager.DeleteLeftLink(roleId, Convert.ToInt32(hih.Value));
                    }
                }

                CheckBoxList chklst = (CheckBoxList)ct.FindControl("chkChildMenu");
                foreach (ListItem li in chklst.Items)
                {
                    if (li.Selected)
                    {
                        if (!arrRoleNodes.Contains(li.Value))
                        {
                            LeftLinkManager.AddLeftLink(roleId, Convert.ToInt32(li.Value), Convert.ToInt32(hih.Value));
                        }
                    }
                    else
                    {
                        if (arrRoleNodes.Contains(li.Value))
                        {
                            LeftLinkManager.DeleteLeftLink(roleId, Convert.ToInt32(li.Value));
                        }
                    }
                }
            }


            //日志
            Models.AdminLog log = new Models.AdminLog();
            int logId = Convert.ToInt32(Session["admin"].ToString());
            string logName = AdminManager.GetAdminByAdminID(logId).AdminName;
            log.AdminName = logName;
            log.LogTime = DateTime.Now;
            log.LogDesc = "修改管理员" + lblAdminName.Text + "的权限。";
            AdminLogManager.AddAdminLog(log);


            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('成功修改管理员权限');", true);
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("Competence.aspx");
        }
    }
}
